package com.simpleaccount.Graph;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.simpleaccount.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/12.
 */

public class GraphFragment extends Fragment {
    @Bind(R.id.iv_lastMonth)
    ImageView ivLastMonth;
    @Bind(R.id.iv_nextMonth)
    ImageView ivNextMonth;
    @Bind(R.id.tv_columnChart)
    TextView tvColumnChart;
    @Bind(R.id.tv_pieChart)
    TextView tvPieChart;
    @Bind(R.id.view_center)
    View viewCenter;

    @Bind(R.id.ll_bottom_graph)
    LinearLayout llBottomGraph;
    @Bind(R.id.fragment_graph)
    FrameLayout fragment_graph;
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_income)
    TextView tvIncome;
    @Bind(R.id.tv_outcome)
    TextView tvOutcome;

    private ColumnChartFragment fmColChart;
    private PieChartFragment fmPieChart;
    private FragmentManager fManager;
    private Calendar calendar;
    private int stat=1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_graph, container, false);
        ButterKnife.bind(this, view);
        tvIncome.setBackgroundResource(R.drawable.shape_graph_left_1);
        tvIncome.setTextColor(getResources().getColor(R.color.杜若色));
        tvOutcome.setBackgroundResource(R.drawable.shape_graph_right_2);
        tvOutcome.setTextColor(getResources().getColor(R.color.grey));
        fManager = getChildFragmentManager();
        initDate();
        clickMenu(0);
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.iv_lastMonth, R.id.iv_nextMonth, R.id.tv_columnChart, R.id.tv_pieChart,R.id.tv_income,R.id.tv_outcome})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_lastMonth:
                clickMonth(-1);
                break;
            case R.id.iv_nextMonth:
                clickMonth(1);
                break;
            case R.id.tv_columnChart:
                clickMenu(0);
                tvColumnChart.setBackgroundResource(R.drawable.shape_graph_left_1);
                tvColumnChart.setTextColor(getResources().getColor(R.color.杜若色));
                tvPieChart.setBackgroundResource(R.drawable.shape_graph_right_2);
                tvPieChart.setTextColor(getResources().getColor(R.color.grey));
                break;
            case R.id.tv_pieChart:
                clickMenu(1);
                tvColumnChart.setBackgroundResource(R.drawable.shape_graph_left_2);
                tvColumnChart.setTextColor(getResources().getColor(R.color.grey));
                tvPieChart.setBackgroundResource(R.drawable.shape_graph_right_1);
                tvPieChart.setTextColor(getResources().getColor(R.color.杜若色));
                break;
            case R.id.tv_income:
                tvIncome.setBackgroundResource(R.drawable.shape_graph_left_1);
                tvIncome.setTextColor(getResources().getColor(R.color.杜若色));
                tvOutcome.setBackgroundResource(R.drawable.shape_graph_right_2);
                tvOutcome.setTextColor(getResources().getColor(R.color.grey));
                stat=1;
                clickMonth(0);
                break;
            case R.id.tv_outcome:
                tvIncome.setBackgroundResource(R.drawable.shape_graph_left_2);
                tvIncome.setTextColor(getResources().getColor(R.color.grey));
                tvOutcome.setBackgroundResource(R.drawable.shape_graph_right_1);
                tvOutcome.setTextColor(getResources().getColor(R.color.杜若色));
                stat=0;
                clickMonth(0);

                break;
        }
    }

    public void clickMenu(int vID) {
        FragmentTransaction trans = fManager.beginTransaction();
        hideFragment(trans);
        setFragment(vID, trans);
        trans.commit();
    }

    public void clickMonth(int month) {
        FragmentTransaction trans = fManager.beginTransaction();
        int vID = 0;
        if (null == fmPieChart || fmPieChart.isHidden()) {
            vID = 0;
        } else if (null == fmColChart || fmColChart.isHidden()) {
            vID = 1;
        }
        hideFragment(trans);
        switch (vID) {
            case 0:
                if (null == fmColChart) {
                    fmColChart = ColumnChartFragment.newInstance(setDate(month),stat);
                    trans.add(R.id.fragment_graph, fmColChart);
                } else {
                    trans.remove(fmColChart);
                    fmColChart = ColumnChartFragment.newInstance(setDate(month),stat);
                    trans.add(R.id.fragment_graph, fmColChart);
                }
                break;
            case 1:
                if (null == fmPieChart) {
                    fmPieChart = PieChartFragment.newInstance(setDate(month),stat);
                    trans.add(R.id.fragment_graph, fmPieChart);
                } else {
                    trans.remove(fmPieChart);
                    fmPieChart = PieChartFragment.newInstance(setDate(month),stat);
                    trans.add(R.id.fragment_graph, fmPieChart);
                }
                break;
            default:
                break;
        }
        trans.commit();
    }

    public void hideFragment(FragmentTransaction trans) {
        if (null != fmColChart) {
            trans.hide(fmColChart);
        }
        if (null != fmPieChart) {
            trans.hide(fmPieChart);
        }
    }

    private void setFragment(int vID, FragmentTransaction trans) {
        switch (vID) {
            case 0:
                if (null == fmColChart) {
                    fmColChart = ColumnChartFragment.newInstance(setDate(0),stat);
                    trans.add(R.id.fragment_graph, fmColChart);
                } else {
                    trans.remove(fmColChart);
                    fmColChart = ColumnChartFragment.newInstance(setDate(0),stat);
                    trans.add(R.id.fragment_graph, fmColChart);

                }
                break;
            case 1:
                if (null == fmPieChart) {
                    fmPieChart = PieChartFragment.newInstance(setDate(0),stat);
                    trans.add(R.id.fragment_graph, fmPieChart);
                } else {
                    trans.remove(fmPieChart);
                    fmPieChart = PieChartFragment.newInstance(setDate(0),stat);
                    trans.add(R.id.fragment_graph, fmPieChart);
                }
                break;
            default:
                break;
        }
    }

    private long initDate() {
        calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        tvDate.setText(sdf.format(calendar.getTime()));
        return calendar.getTimeInMillis();
    }

    private long setDate(int month) {
        if (1 == month) {
            calendar.add(Calendar.MONTH, 1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            tvDate.setText(sdf.format(calendar.getTime()));
        } else if (-1 == month) {
            calendar.add(Calendar.MONTH, -1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            tvDate.setText(sdf.format(calendar.getTime()));
        } else if (0 == month) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            tvDate.setText(sdf.format(calendar.getTime()));
        }
        return calendar.getTimeInMillis();
    }


}
